package org.wangblue.document;

import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.wangblue.common.Client;

import java.io.IOException;

/**
 * @Description: 查询数据
 * @Author: WangBlue
 * @Create: 2024-10-24 09:32
 * @Version: 1.0
 **/

public class GetDocument {
    private static final String INDEX_NAME = "user";
    private static final String ID = "1001";
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = Client.getClient();
        try {
            // 1. 新增文档 - 请求对象
            GetRequest request = new GetRequest();

            // 2. 设置索引名称
            request.index(INDEX_NAME).id(ID);

            // 3. 向客户端发送请求， 获取响应对象
            GetResponse response = client.get(request, RequestOptions.DEFAULT);

            // 4. 解析响应对象， 打印结果信息
            System.out.println("_index:" + response.getIndex());
            System.out.println("_type:" + response.getType());
            System.out.println("_id:" + response.getId());
            System.out.println("source:" + response.getSourceAsString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            client.close();
        }

    }
}
